﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using Travianer.Common;
using System.IO;
using Travianer.Model;
using System.IO.Compression;

namespace Travianer.Handler
{
    public class HttpWebRequestHandler : Travianer.IHandler.IHandler
    {

        private HttpWebResponse GetResponse(string url)
        {
            return GetResponse(url, false);
        }
        private HttpWebResponse GetResponse(string url, bool IsPost, params object[] para)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Timeout = 20 * 1000; //连接超时
            request.Referer = url;
            request.Accept = "image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, */*";
            request.Headers.Add("Accept-Language", "en-GB,en;q=0.8,zh-Hans-CN;q=0.5,zh-Hans;q=0.3");
            request.Headers.Add("Accept-Encoding", "gzip,deflate,sdch");
            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.2; WOW64; Trident/6.0; .NET4.0E; .NET4.0C; InfoPath.3; .NET CLR 3.5.30729; .NET CLR 2.0.50727; .NET CLR 3.0.30729)";
            request.KeepAlive = true;
            request.ContentType = "application/x-www-form-urlencoded";
            request.CookieContainer = CookieHelper.GetInstance().Cookies;
            request.AllowAutoRedirect = true;

            if (IsPost)
            {
                request.Method = "POST";
                byte[] postData = Configuration.Encoding.GetBytes(para[0].ToString());
                request.ContentLength = postData.Length;

                Stream stream;
                stream = request.GetRequestStream();
                stream.Write(postData, 0, postData.Length);
                stream.Close();
            }

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            response.Cookies = CookieHelper.GetInstance().Cookies.GetCookies(request.RequestUri);

            return response;
        }
        public ResponseContainer Login(string strServerLocation, string strUserName, string strPassword, params object[] para)
        {
            ResponseContainer ent = new ResponseContainer();

            HttpWebResponse response = GetResponse(strServerLocation, true, string.Format(Configuration.LoginString, strUserName, strPassword));
            ent.Url = response.ResponseUri.AbsolutePath;
            using (StreamReader reader = new StreamReader(new GZipStream(response.GetResponseStream(), CompressionMode.Decompress), Configuration.Encoding))
            {
                ent.SourceCode = reader.ReadToEnd();
            }

            return ent;
        }
        public ResponseContainer Get(string url)
        {
            ResponseContainer ent = new ResponseContainer();

            HttpWebResponse response = GetResponse(url);
            ent.Url = response.ResponseUri.AbsolutePath;
            if (response.StatusCode == HttpStatusCode.OK)
            {
                using (StreamReader reader = new StreamReader(new GZipStream(response.GetResponseStream(), CompressionMode.Decompress), Configuration.Encoding))
                {
                    ent.SourceCode = reader.ReadToEnd();
                }
            }

            return ent;
        }
        public ResponseContainer Post(string url, string data)
        {
            ResponseContainer ent = new ResponseContainer();

            HttpWebResponse response = GetResponse(url, true, data);
            ent.Url = response.ResponseUri.AbsolutePath;
            if (response.StatusCode == HttpStatusCode.OK)
            {
                using (StreamReader reader = new StreamReader(new GZipStream(response.GetResponseStream(), CompressionMode.Decompress), Configuration.Encoding))
                {
                    ent.SourceCode = reader.ReadToEnd();
                }
            }

            return ent;
        }
    }
}
